﻿#pragma once


#include<iostream>

using namespace std;

enum Colour
{
	RED,
	BLACK
};

template<class K, class V>
struct RBTreeNode
{
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;
	pair<K, V> _kv;
	Colour _col;

	RBTreeNode(const pair<K, V>& kv)
		:_left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _kv(kv)
	{ }
};

template<class K, class V>
class RBTree
{
	typedef RBTreeNode<K, V> Node;

	void RotateR(Node* parent)
	{
		Node* sub_left = parent->_left;
		Node* sub_left_right = sub_left->_right;
		Node* parent_parent = parent->_parent;

		parent->_left = sub_left_right;
		if (sub_left_right)
		{
			sub_left_right->_parent = parent;
		}

		sub_left->_right = parent;
		parent->_parent = sub_left;

		if (parent_parent == nullptr)
		{
			_root = sub_left;
		}
		else
		{
			if (parent == parent_parent->_left)
			{
				parent_parent->_left = sub_left;
			}
			else
			{
				parent_parent->_right = sub_left;
			}
		}
		sub_left->_parent = parent_parent;
	}

	void RotateL(Node* parent)
	{
		Node* sub_right = parent->_right;
		Node* sub_right_left = sub_right->_left;
		Node* parent_parent = parent->_parent;

		parent->_right = sub_right_left;
		if (sub_right_left)
		{
			sub_right_left->_parent = parent;
		}

		sub_right->_left = parent;
		parent->_parent = sub_right;

		if (parent_parent == nullptr)
		{
			_root = sub_right;
		}
		else
		{
			if (parent == parent_parent->_left)
			{
				parent_parent->_left = sub_right;
			}
			else
			{
				parent_parent->_right = sub_right;
			}
		}
		sub_right->_parent = parent_parent;
	}

	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_InOrder(root->_left);
		cout << root->_kv.first << ' ';
		_InOrder(root->_right);
	}

	bool Check(Node* root, int black_sum, int reference_val)
	{
		if (root == nullptr)
		{
			if (black_sum != reference_val)
			{
				return false;
			}

			return true;
		}

		if (root->_col == RED && root->_parent && root->_parent->_col == RED)
		{
			cout << root->_kv.first << "存在连续的红⾊结点" << endl;
			return false;
		}

		if (root->_col == BLACK)
		{
			black_sum++;
		}

		return Check(root->_left, black_sum, reference_val) 
			&& Check(root->_right, black_sum, reference_val);
	}

public:
	bool Insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK;
			return true;
		}

		Node* cur = _root;
		Node* parent = nullptr;

		while (cur)
		{
			if (kv.first > cur->_kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (kv.first < cur->_kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}

		cur = new Node(kv);
		cur->_col = RED;
		if (cur->_kv.first > parent->_kv.first)
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		cur->_parent = parent;

		while (parent && parent->_col == RED)
		{
			Node* parent_parent = parent->_parent;
			if (parent == parent_parent->_left)
			{
				Node* uncle = parent_parent->_right;
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					parent_parent->_col = RED;

					cur = parent_parent;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_left)
					{
						RotateR(parent_parent);

						parent->_col = BLACK;
						parent_parent->_col = RED;
					}
					else
					{
						RotateL(parent);
						RotateR(parent_parent);

						cur->_col = BLACK;
						parent_parent->_col = RED;
					}
					break;
				}
			}
			else
			{
				Node* uncle = parent_parent->_left;
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					parent_parent->_col = RED;

					cur = parent_parent;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_left)
					{
						RotateR(parent);
						RotateL(parent_parent);

						cur->_col = BLACK;
						parent_parent->_col = RED;
					}
					else
					{
						RotateL(parent_parent);

						parent->_col = BLACK;
						parent_parent->_col = RED;
					}
					break;
				}
			}
		}

		_root->_col = BLACK;

		return true;
	}

	Node* Find(const K& key)
	{
		Node* cur = _root;

		while (cur)
		{
			if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else
			{
				return cur;
			}
		}
		
		return nullptr;
	}

	bool IsBalance()
	{
		if (_root == nullptr)
		{
			return true;
		}

		if (_root->_col == RED)
		{
			return false;
		}

		int reference_val = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				reference_val++;
			}
			cur = cur->_left;
		}

		return Check(_root, 0, reference_val);
	}

	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

private:
	Node* _root = nullptr;
};